package com.dreamdance.queerball;

import net.ds0101.framework.android.neox.action.NXAction;
import net.ds0101.framework.android.neox.action.NXFadeOut;
import net.ds0101.framework.android.neox.action.NXParallel;
import net.ds0101.framework.android.neox.action.NXRepeatForever;
import net.ds0101.framework.android.neox.action.NXRotate;
import net.ds0101.framework.android.neox.action.NXZoom;
import net.ds0101.framework.android.neox.animation.NXAbstractAnimation;
import net.ds0101.framework.android.neox.animation.NXAbstractAnimation.AnimationListener;
import net.ds0101.framework.android.neox.animation.NXAbstractAnimation.State;
import net.ds0101.framework.android.neox.animation.NXPropertyAnimation;
import net.ds0101.framework.android.neox.core.NXNode;
import net.ds0101.framework.android.neox.main.NXDirector;
import net.ds0101.framework.android.neox.main.NXLayer;
import net.ds0101.framework.android.neox.util.LogUtil;
import net.ds0101.framework.android.neox.util.NXEasingCurve;
import net.ds0101.framework.android.neox.util.NXResourceConverter;
import net.ds0101.framework.android.neox.widget.NXSprite;

public class GameOverUI extends NXLayer {
	private QueerBallGameScene mScene;
	private NXSprite mRetry;
	private NXAction mSpring;
	private NXAction mClose;
	
	public GameOverUI(QueerBallGameScene scene) {
		super(NXResourceConverter.getBitmapFromResource(scene, R.drawable.gameover));
		setAntiAlias(true);
		mScene = scene;
		mSpring = NXRepeatForever.action(new NXZoom(850, 1.0f, 1.1f, NXEasingCurve.Type.SineCurve));
		mClose = new NXParallel(new NXRotate(600, 0f, 360f), NXFadeOut.action(600), new NXZoom(600, 1.0f, 0f));
		NXAbstractAnimation anim = mClose.getAnimation();
		anim.setAnimationListener(new AnimationListener(){
			@Override
			public void onAnimationStart(NXAbstractAnimation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationFinish(NXAbstractAnimation animation) {
				((NXPropertyAnimation)animation).target().visible(false);
				LogUtil.i("QueerBall", "Game Over closed.");
			}

			@Override
			public void onAnimationRepeat(NXAbstractAnimation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationStateChanged(NXAbstractAnimation animation,
					State newState, State oldState) {
				// TODO Auto-generated method stub
			}
			
		});
		mRetry = new NXSprite(NXResourceConverter.getBitmapFromResource(mScene, R.drawable.again_press));
		mRetry.position(width() * 0.65f, height() * 0.8f);
		mRetry.setOnClickListener(new OnClickCallback() {
			@Override
			public void onClick(NXNode node) {
				mScene.gameStart();
			}
		});
		addChild(mRetry);
		position(NXDirector.screenCenter());
	}
	
	public void close() {
		runAction(mClose);
	}
	
	@Override
	protected void onShow() {
		rotate(0);
		scale(1.0f);
		mRetry.runAction(mSpring);
	}
	
	@Override
	protected void onHide() {
		mRetry.stopAction(mSpring);
	}
}
